﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
using System.ComponentModel;

namespace Fonlow.CommandLineGui.Robocopy
{
    /// <summary>
    /// Model of Robocopy command
    /// </summary>
    public class RobocopyCommand : CommandBase
    {
        public RobocopyCommand() : base(new  Options())
        {

        }

        public override string CommandName
        {
            get { return "Robocopy"; }
        }

        string source;
        string Source
        {
            get
            {
                if (String.IsNullOrEmpty(source))
                    return "Source";
                return source;
            }
            set { source = value; }
        }

        string destination;
        string Destination
        {
            get
            {
                if (String.IsNullOrEmpty(destination))
                    return "Destination";
                return destination;
            }

            set { destination = value; }
        }

        string Files { get; set; }
        
        public override bool Load(string commandLine)
        {
            Regex topSegments = new Regex(Properties.Resources.TOP_SEGMENTS, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            Match m = topSegments.Match(commandLine);
            if (m.Success)
            {
                if (m.Groups["Source"].Success)
                {
                    Source = m.Groups["Source"].Value;
                }
                else
                    return false;

                if (m.Groups["Dest"].Success)
                {
                    Destination = m.Groups["Dest"].Value;
                }
                else
                    return false;

                if (m.Groups["Files"].Success)
                {
                    Files = m.Groups["Files"].Value.Trim();
                }

                if (m.Groups["Options"].Success)
                {
                    ResetParameters();
                    ParseParameters(m.Groups["Options"].Value);
                }

                return true;
            }
            else
                return false;
        }

        private void ParseParameters(string parameters)
        {
            Regex parametersRegex = new Regex(Properties.Resources.PARAMETERS, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
            MatchCollection matches = parametersRegex.Matches(parameters);
            foreach (Match m in matches)
            {
                if (m.Groups["Name"].Success)
                {
                    string parameterName = m.Groups["Name"].Value;
                    string parameterValue = String.Empty;
                    if (m.Groups["Rest"].Success)
                        parameterValue = m.Groups["Rest"].Value.Trim();

                    AssignParameter(parameterName, parameterValue);
                }
            }
        }

        public override string PickupParameterAtPosition(string commandText, int position)
        {
            if (commandText == null)
                throw new ArgumentNullException("commandText");

            int slashPosition = -1;
            for (int i = position; i > 0; i--)
            {
                if (commandText[i] == '/')
                {
                    slashPosition = i;
                    break;
                }
            }

            if (slashPosition > -1)
            {
                Regex parametersRegex = new Regex(Properties.Resources.PARAMETERS, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
                Match m = parametersRegex.Match(commandText.Substring(slashPosition));
                if (m.Success)
                {
                    if (m.Groups["Name"].Success)
                    {
                        string parameterName = m.Groups["Name"].Value;
                        return parameterName;
                    }
                }

            }
            return null;
        }

        public override string Parameters
        {
            get
            {
                OptionsBuilder builder = new OptionsBuilder(CommandParameters);
                return Source + " " + Destination + " " + Files + " " +
                    builder.ParametersLine;

            }
        }

        public override string CommandLine
        {
            get
            {
                return CommandName + " " + Parameters;
            }
        }
    }
}
